package com.gdyx.bc.gw.web;

import com.gdyx.bc.gw.sdk.unionpay.AcpService;
import com.gdyx.bc.gw.sdk.unionpay.LogUtil;
import com.gdyx.bc.gw.sdk.unionpay.SDKConfig;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by mac on 2017/5/13.
 */
@Controller
@RequestMapping("/C2B")
public class C2BController {
    /**
     * C2B码申请
     * @param accNo
     * @param name
     * @param mobile
     * @return
     */
    @RequestMapping(value = "ApplyQrNo")
    @ResponseBody
    public String ApplyQrNo(String cardAttr,String accNo,String name,String mobile){
        String issCode = "90880020";


        String backUrl ="";

        String pinFree = "2000";
        String maxAmont ="2000";
        String addnOpUrl = "";

        Map<String, String> contentData = new HashMap<>();

        contentData.put("version", "1.0.0");
        contentData.put("reqType", "0210000903");
        contentData.put("issCode", issCode);
        contentData.put("qrType", "35"); //35借记卡   51 贷记卡

        Map<String, String> payerInfoMap = new HashMap<String, String>();
        payerInfoMap.put("accNo", accNo);
        if(null!=name && !"".equals(name)) payerInfoMap.put("name", name);
        payerInfoMap.put("acctClass", "1");
        payerInfoMap.put("mobile", mobile);//手机号必送
        payerInfoMap.put("cardAttr", cardAttr);//01 – 借记卡 02 – 贷记卡（含准贷记卡）

        //如果对机构号issCode 配置了敏感信息加密，必须
        //1.送encryptCertId 2.对payerInfo，payeeInfo的值整体加密

        //敏感信息不加密使用DemoBase.getPayerInfo方法
        contentData.put("payerInfo", BaseController.getPayerInfo(payerInfoMap,"UTF-8"));

        //敏感信息加密使用 DemoBase.getPayerInfoWithEncrpyt方法
        //contentData.put("payerInfo", DemoBase.getPayerInfoWithEncrpyt(payerInfoMap,"UTF-8"));
        //敏感信息加密增加encryptCertId字段
        //contentData.put("encryptCertId", AcpService.getEncryptCertId());

        if("".equals(pinFree.trim()) && "".equals(maxAmont.trim()) && "".equals(addnOpUrl.trim()))
        {
            //不送addnCond addnOpUrl
        }else{
            Map<String, String> addnCondMap = new HashMap<String, String>();
            addnCondMap.put("currency", "156"); //金额币种
            addnCondMap.put("pinFree", pinFree);//免密限额
            addnCondMap.put("maxAmont", maxAmont);//最高交易金额
            contentData.put("addnCond", BaseController.getAddnCond(addnCondMap,"UTF-8")); //附加处理条件
            contentData.put("addnOpUrl", addnOpUrl);//附加处理服务器地址
        }
        contentData.put("reqReserved", "reserved"+BaseController.getOrderId());
        //c2b交易通知发送地址
        contentData.put("backUrl", backUrl);

        Map<String, String> reqData = AcpService.sign(contentData,BaseController.encoding);			 //报文中certId,signature的值是在signData方法中获取并自动赋值的，只要证书配置正确即可。
        String requestUrl = SDKConfig.getConfig().getQrcB2cIssBackTransUrl();
        Map<String, String> rspData = AcpService.post(reqData,requestUrl,BaseController.encoding);  //发送请求报文并接受同步应答（默认连接超时时间30秒，读取返回结果超时时间30秒）;这里调用signData之后，调用submitUrl之前不能对submitFromData中的键值对做任何修改，如果修改会导致验签不通过

        if(!rspData.isEmpty()){
            if(AcpService.validate(rspData, BaseController.encoding)){
                LogUtil.writeLog("验证签名成功");
                String respCode = rspData.get("respCode") ;
                if(("00").equals(respCode)){
                    return rspData.get("qrNo");
                }else{
                    //其他应答码为失败请排查原因
                    //TODO
                }
            }else{
                LogUtil.writeErrorLog("验证签名失败");
                //TODO 检查验证签名失败的原因
            }
        }else{
            //未返回正确的http状态
            LogUtil.writeErrorLog("未获取到返回报文或返回http状态码非200");
        }
        String reqMessage = BaseController.genHtmlResult(reqData);
        String rspMessage = BaseController.genHtmlResult(rspData);
        return "</br>请求报文:<br/>"+reqMessage+"<br/>" + "应答报文:</br>"+rspMessage+"";
    }
}
